/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame;

import com.jme3.app.Application;
import com.jme3.app.state.AppStateManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.HeightfieldCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.light.DirectionalLight;
import com.jme3.light.Light;
import com.jme3.post.FilterPostProcessor;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.shadow.DirectionalLightShadowFilter;
import com.jme3.shadow.DirectionalLightShadowRenderer;
import com.jme3.terrain.geomipmap.TerrainQuad;

/**
 *
 * @author Paulo
 */
public class SceneState extends SimpleAppState {

    private Spatial s;
    
    private BulletAppState bullet;
    private PlayerAppState player;

    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);

        bullet = new BulletAppState();
        this.app.getStateManager().attach(bullet);

        s = setupScene("Scenes/Scene.j3o");

        this.app.getRootNode().attachChild(s);
        
        player = new PlayerAppState(s);
        app.getStateManager().attach(player);
    }

    private Spatial setupScene(String sceneName) {
        Spatial scene = app.getAssetManager().loadModel(sceneName);
        TerrainQuad tq = ((TerrainQuad) ((Node) scene).getChild("terrain-Scene"));

        HeightfieldCollisionShape terrainCollision = new HeightfieldCollisionShape(tq.getHeightMap());
        RigidBodyControl terrainCollisionControl = new RigidBodyControl(terrainCollision, 0);
        tq.addControl(terrainCollisionControl);
        bullet.getPhysicsSpace().add(tq);

        DirectionalLight sun = null;
        for (Light l : scene.getLocalLightList()) {
            if (l.getType() == Light.Type.Directional) {
                sun = (DirectionalLight) l;
            }
        }
        
        final int SHADOWMAP_SIZE = 1 * 1024;
        final int SPLITS = 1;
        
        DirectionalLightShadowRenderer shadowRenderer = new DirectionalLightShadowRenderer(app.getAssetManager(), SHADOWMAP_SIZE, SPLITS);
        shadowRenderer.setLight(sun);
        app.getViewPort().addProcessor(shadowRenderer);
        
        //Essa parte n ta funcionando no meu pc
        //DirectionalLightShadowFilter shadowFilter = new DirectionalLightShadowFilter(app.getAssetManager(), SHADOWMAP_SIZE, SPLITS);
        //shadowFilter.setEnabled(true);
        //shadowFilter.setLight(sun);
        //FilterPostProcessor fpp = new FilterPostProcessor(app.getAssetManager());
        //fpp.addFilter(shadowFilter);
        //app.getViewPort().addProcessor(fpp);

        return scene;
    }

    @Override
    public void update(float tpf) {
        super.update(tpf);
    }

    @Override
    public void cleanup() {
        super.cleanup();

        s.removeFromParent();
    }
}
